package com.thunder.ktv.scoreengine2test;

import android.app.Presentation;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.thunder.ktv.scoreengine2sdk.DifficultyLevel;
import com.thunder.ktv.scoreengine2test.ui.LyricsView;
import com.thunder.ktv.scoreengine2test.ui.PitchView;
import com.thunder.ktv.scoreengine2test.ui.ScoreBarView;
import com.thunder.ktv.scoreengine2test.ui.ScoreRadarChartView;
import com.thunder.ktv.scoreengine2test.ui.SpectrumView2;
import com.thunder.ktv.scoreengine2test.utils.FileUtil;

import java.io.File;

public class TVPresentation extends Presentation {

    private final Context mContext;
    private final Display display;
    private final TextView titleView;
    private final TextView infoView;
    private final SurfaceView surfaceView;
    private PitchView pitchView = null;
    private final ScoreBarView scoreBarView = null;
    private final ScoreRadarChartView scoreRadarChartView;

    private SpectrumView2 originalSpectrumView;
    private SpectrumView2 audioSpectrumView;

    // --- 新增：用于存储分数的成员变量 ---
//    private int mAllScore = 0;
//    private int mHighScoreCount = 0;
//    private int mOverallScore = 0;
    private LyricsView lyricsView;
    private Song selectedSong;

    public TVPresentation(Context outerContext, Display display) {
        super(outerContext, display);

        this.display = display;
        mContext = outerContext;
        // 确保你的布局文件中包含这些ID
        setContentView(R.layout.tv_display);
        surfaceView = findViewById(R.id.surfaceView);
        titleView = findViewById(R.id.title_info_view);
        lyricsView = findViewById(R.id.tv_lyrics);
        infoView = findViewById(R.id.info_view);
//        pitchView = findViewById(R.id.tv_pitch_view);
//        scoreBarView = findViewById(R.id.tv_score_bar_view);

        originalSpectrumView = findViewById(R.id.tv_original_spectrum);
        audioSpectrumView = findViewById(R.id.tv_audio_spectrum);
        scoreRadarChartView = findViewById(R.id.tv_score_radar_chart_view);
    }

    public void setSelectedSong(Song selectedSong) {
        this.selectedSong = selectedSong;
        // 初始化显示
        updateScoreDisplay();
    }

    int difficultyLevel = 0;

    public void setDifficultyLevel(int difficultyLevel) {
        this.difficultyLevel = difficultyLevel;
        updateScoreDisplay();
    }

    public SpectrumView2 getAudioSpectrumView() {
        return audioSpectrumView;
    }

    public SpectrumView2 getOriginalSpectrumView() {
        return originalSpectrumView;
    }

    public ScoreRadarChartView getScoreRadarChartView() {
        return scoreRadarChartView;
    }

    public LyricsView getLyricsView() {
        return lyricsView;
    }

    public PitchView getPitchView() {
        return pitchView;
    }

    public ScoreBarView getScoreBarView() {
        return scoreBarView;
    }

    //    public void setOsdTitle(String osdTitle) {
//        this.osdTitle = osdTitle;
//        updateScoreDisplay();
//    }
//
//    //歌曲总分
//    public void setAllScore(int score) {
//        mAllScore = score;
//        updateScoreDisplay();
//    }
//
//    //获得高分次数
//    public void setHighScoreCount(int count) {
//        mHighScoreCount = count;
//        updateScoreDisplay();
//    }
//
//    //当前一句的分数
//    public void setOverallScore(int score) {
//        mOverallScore = score;
//        updateScoreDisplay();
//    }

    /**
     * 核心方法：更新TextView的显示内容
     * 这个方法会确保UI更新操作在主线程执行
     */
    private void updateScoreDisplay() {
        // 1. 格式化需要显示的字符串
//        final String scoreText = String.format(
//                "总分: %d\n高分次数: %d\n本句得分: %d",
//                mAllScore, mHighScoreCount, mOverallScore
//        );
//
//        // 2. 确保在UI线程更新TextView
//        if (textView != null) {
//            // View.post()方法可以将一个Runnable任务投递到UI线程的消息队列中
//            // 这样无论当前在哪个线程调用，setText都会在UI线程执行，是安全且推荐的做法
//            textView.post(() -> textView.setText(scoreText));
//        }
        if(titleView != null){
            titleView.post(() -> titleView.setText("当前播放:" + selectedSong.getTitle()));
        }
        if (infoView != null) {
            infoView.post(() -> {
                // 1. 从数值获取难度等级枚举实例
                DifficultyLevel level = DifficultyLevel.fromValue(difficultyLevel);
                // 2. 设置文本内容
                infoView.setText("当前难度:" + level);

                int colorID = R.color.difficulty_easy;
                if(level == DifficultyLevel.NORMAL){
                    colorID = R.color.difficulty_normal;
                }else if(level == DifficultyLevel.HARD){
                    colorID = R.color.difficulty_hard;
                }else if(level == DifficultyLevel.EXPERT){
                    colorID = R.color.difficulty_expert;
                }
                // 3. 从枚举中获取颜色资源ID，并设置颜色
                int textColor = ContextCompat.getColor(infoView.getContext(), colorID);
                infoView.setTextColor(textColor);
            });
        }
    }
    public SurfaceView getSurfaceView() {
        return surfaceView;
    }
}